<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use app\common\model\Menu;
use app\common\model\Role;
use think\facade\Cache;
use think\facade\View;

class Index extends Base
{
    protected array $sort = [];

    /**
     * 显示资源列表
     *
     * @return string
     */
    public function index(): string
    {
        $rules = '';
        if ($this->admin['role_id'] != 1) {
            $rules = (new Role())->where('id', $this->admin['role_id'])->value('rules');
        }

        View::assign([
            'menus'      => (new Menu())->getMenu($rules),
            'attachMenu' => Builder::TableCache()->getAttachMenu()
        ]);

        return View::fetch();
    }

    public function welcome(): string
    {
        View::assign('server_name', $_SERVER['SERVER_NAME']);
        return View::fetch();
    }

    public function clear_cache(): \think\response\Json
    {
        return Cache::clear() ? json(['code' => 1, 'msg' => '已清除']) : json(['code' => -1, 'msg' => '加载失败']);
    }
}
